package thread;

import java.util.concurrent.locks.ReentrantLock;

// ReentrantLock (可重入的)的使用
public class Demo31 {
    private static  int count = 0;
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();//可以变成 公平锁 进行传值 —— 传 ture
        //使用 lock和unlock 进行加锁解锁
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    reentrantLock.lock();
                    count++;
                    reentrantLock.unlock();
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    reentrantLock.lock();
                    count++;
                    reentrantLock.unlock();
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("count = " + count);
    }
}
